package edu.scau.mis.web.controller.pos;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import edu.scau.mis.core.annotation.RequestLog;
import edu.scau.mis.core.domain.AjaxResult;
import edu.scau.mis.core.enums.BusinessType;
import edu.scau.mis.pos.domain.Product;
import edu.scau.mis.pos.dto.ProductSkusDTO;
import edu.scau.mis.pos.service.IStoreService;
import edu.scau.mis.pos.vo.ProductSkusVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("store")
@Tag(name = "StoreController ", description = "库存管理")
public class StoreController {
    @Autowired
    private IStoreService storeService;

    @RequestLog(title = "查询所有库存数据",businessType = BusinessType.Query)
    @GetMapping("/listAll")
    @Operation(summary = "库存数据" ,
            description = "查询所有库存数据")
    public AjaxResult getProductAndSkuDetails() {
        // 调用 Mapper 方法
        return AjaxResult.success(storeService.selectProductAndSkuDetails());
    }

    @RequestLog(title = "查询分页库存数据",businessType = BusinessType.Query)
    @PostMapping("/page")
    @Operation(summary = "分页查询" ,
            description = "传入需要查询的字段")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,  @RequestBody ProductSkusVo productSkusVo) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductSkusDTO> list = storeService.selectProductAndSkuDetailsByCondition(productSkusVo);
        PageInfo<ProductSkusDTO> pageInfo = new PageInfo(list);
        System.out.println(productSkusVo);
        return AjaxResult.success(pageInfo);
    }

    @RequestLog(title = "查询库存数据",businessType = BusinessType.Query)
    @PostMapping("/Condition")
    @Operation(summary = "根据条件查询数据" ,
            description = "传入需要查询的字段（编码、名称、规格、上架状态）")
    public AjaxResult getProductAndSkuDetailsBySn(@RequestBody ProductSkusVo productSkusVo) {
        // 调用 Mapper 方法
        return AjaxResult.success(storeService.selectProductAndSkuDetailsByCondition(productSkusVo));
    }

    @RequestLog(title = "切换上架状态",businessType = BusinessType.OTHER)
    @PostMapping("/{skuCode}")
    @Operation(summary = "切换上架状态",
            description = "传入skuCode编码，实现自动切换状态",
            parameters = {
            @Parameter(name = "skuCode", description = "规格编码")
    })
    public AjaxResult toggleIsAvailableBySkuCode(@PathVariable String skuCode){
        int row = storeService.toggleIsAvailableBySkuCode(skuCode);
        return row > 0 ? AjaxResult.success("状态切换成功") : AjaxResult.error("状态切换失败");
    }

    @RequestLog(title = "更新库存",businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @Operation(summary = "更新库存",
            description = "传入需要更新的字段")
    public AjaxResult updateStockBySkuCode(@RequestBody ProductSkusDTO productSkusDTO){
        int row = storeService.updateStockBySkuCode(productSkusDTO);
        return row > 0 ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
    }

    @RequestLog(title = "添加库存数据",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Operation(summary = "添加库存数据" ,
            description = "传入需要的字段（productSn,size,color,price,status,skuUrl,stockQuantity）")
    public AjaxResult insertStock(@RequestBody ProductSkusDTO productSkusDTO) {
        // 调用 Mapper 方法
        return AjaxResult.success(storeService.insertStock(productSkusDTO));
    }
    @RequestLog(title = "删除库存",businessType = BusinessType.DELETE)
    @DeleteMapping("/{skuCode}")
    @Operation(summary = "单个删除",description = "根据skuCode删除",
            parameters = {
                    @Parameter(name = "skuCode", description = "规格编码")
            })
    public AjaxResult remove(@PathVariable String skuCode) {
        int rows = storeService.deleteProductAndSkuDetailsByCode(skuCode);
        return rows > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
    @RequestLog(title = "批量删除库存",businessType = BusinessType.DELETE)
    @DeleteMapping("/delBatch/{skuCodes}")
    @Operation(summary = "批量删除",description = "根据skuCode集合删除",
            parameters = {
                    @Parameter(name = "skuCodes", description = "规格编号集合")
            })
    public AjaxResult remove(@PathVariable String[] skuCodes) {
        int rows = storeService.deleteProductAndSkuDetailsByCodes(skuCodes);
        return rows > 0 ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }


}

